package com.wgj.android.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jiaai.xui.utils.http.ListHttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.adapter.ShopStoreGridAdapter;
import com.wgj.android.entity.ShopStore;
import com.wgj.android.http.WGJAPI;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/5/5.
 */

public class ShopFragment extends Fragment {
    @BindView(R.id.shop_store_list)
    RecyclerView recyclerView;
    private ShopStoreGridAdapter shopStoreGridAdapter;
    private List<ShopStore> shopStores=new ArrayList<>();
    private int brandId;
    private View view;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

         if(view==null){
             view = inflater.inflate(R.layout.fragment_shop,null);
             ButterKnife.bind(this, view);
             initView();
         }
         ViewGroup viewGroup= (ViewGroup) view.getParent();
         if(viewGroup!=null){
             viewGroup.removeView(view);
         }

        return view;
    }
    private void initView() {
        brandId=getArguments().getInt("brandId");
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
        shopStoreGridAdapter=new ShopStoreGridAdapter(recyclerView,shopStores);
        recyclerView.setAdapter(shopStoreGridAdapter);

        loadShopStores();
    }

    private void loadShopStores() {
        XHttp.obtain().get(WGJAPI.BASEURL + "shop/shopList?brandId="+brandId, null, new ListHttpCallBack<ShopStore>() {
            @Override
            public void onSuccess(List<ShopStore> list) {
                shopStores.clear();
                shopStores.addAll(list);
                shopStoreGridAdapter.notifyDataSetChanged();
            }
            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.error("加载数据失败");
            }
        });
    }


}
